<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>JSON 验证器</title>
    <style>
        /* 通用布局 */
        body {
            font-family: 'Arial', sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f4f7fa;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            color: #333;
        }

        .container {
            background-color: #fff;
            border-radius: 10px;
            padding: 40px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 600px;
            text-align: center;
        }

        h1 {
            font-size: 28px;
            margin-bottom: 20px;
            color: #333;
        }

        p {
            font-size: 16px;
            margin-bottom: 20px;
        }

        textarea {
            width: 100%;
            padding: 12px;
            font-size: 16px;
            border: 1px solid #ccc;
            border-radius: 6px;
            box-sizing: border-box;
            margin-bottom: 20px;
            resize: none;
            transition: border-color 0.3s ease;
        }

        textarea:focus {
            border-color: #007bff;
            outline: none;
        }

        button.btn {
            background-color: #007bff;
            color: white;
            border: none;
            padding: 12px 20px;
            font-size: 16px;
            border-radius: 6px;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }

        button.btn:hover {
            background-color: #0056b3;
        }

        .result {
            margin-top: 20px;
            font-size: 18px;
            padding: 10px;
            border-radius: 6px;
            transition: all 0.3s ease;
        }

        .result.valid {
            background-color: #d4edda;
            color: #155724;
            border: 1px solid #c3e6cb;
        }

        .result.invalid {
            background-color: #f8d7da;
            color: #721c24;
            border: 1px solid #f5c6cb;
        }
    </style>
</head>

<body>
    <div class="container">
        <h1>JSON 验证器</h1>
        <p>请输入一个 JSON 字符串：</p>
        <textarea id="jsonInput" placeholder='请输入 JSON 格式的字符串...' rows="6" cols="50"></textarea><br><br>
        <button onclick="validateJSON()" class="btn">验证 JSON</button>

        <div id="result" class="result"></div>
    </div>

    <script>
        // JSON 验证函数
        const isValidJSON = val => {
            try {
                const res = JSON.parse(val);
                // JSON.parse('null') => null
                return res !== null ? true : false;
            } catch (error) {
                return false;
            }
        };

        // 验证用户输入的 JSON
        function validateJSON() {
            const input = document.getElementById('jsonInput').value;
            const resultDiv = document.getElementById('result');

            if (isValidJSON(input)) {
                resultDiv.textContent = "有效 JSON";
                resultDiv.className = 'result valid';
            } else {
                resultDiv.textContent = "无效 JSON";
                resultDiv.className = 'result invalid';
            }
        }
    </script>
</body>

</html>